package com.example.mybatisplus.demo.easypoi;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.example.mybatisplus.demo.easypoi.dto.LoadBondData;
import com.example.mybatisplus.demo.easypoi.dto.StudentEntity;
import com.example.mybatisplus.demo.easypoi.dto.TestSecuritiesEntity;
import com.example.mybatisplus.demo.easypoi.util.ExcelEasyPoiUtils;
import com.google.common.collect.Lists;
import com.google.gson.JsonArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author zengqingfa
 * @className EasyPoiController
 * @description
 * @create 2022/12/18 20:45
 */
@RestController
@RequestMapping("/easyPoiTest")
@Slf4j
public class EasyPoiTestController {

    @Resource
    private RestTemplate restTemplate;

    public static final String url="http://www.governbond.org.cn:8080/api/loadBondData.action?timeStamp=1675571200968&dataType=ZQFXLISTBYAD&adList=&adCode=87&zqlx=2&year=&fxfs=&qxr=&fxqx=&zqCode=&zqName=&page=1&pageSize=10000";


    /**
     * 下载Excel
     *
     * @param response
     */
    @PostMapping(value = "/downloadExcel")
    public void downloadExcel(HttpServletResponse response) throws Exception {
        //设置get请求参数
        Map<String,String> params = new HashMap<>();
        //设置请求头参数
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Accept", "application/json, text/javascript, */*; q=0.01");
        requestHeaders.add("Accept-Language", "zh-CN,zh;q=0.9");
        requestHeaders.add("Connection", "keep-alive");
//        requestHeaders.add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        requestHeaders.add("Origin", "http://www.celma.org.cn");
        requestHeaders.add("Referer", "http://www.celma.org.cn/");
        requestHeaders.add("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
        HttpEntity request = new HttpEntity(requestHeaders);

        ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET,request, String.class, params);
        String body = exchange.getBody();
        LoadBondData loadBondData = JSON.parseObject(body, LoadBondData.class);
        List<LoadBondData.Item> data = loadBondData.getData();
        List<TestSecuritiesEntity> list= Lists.newArrayList();
        for (LoadBondData.Item item:data) {
            TestSecuritiesEntity securitiesEntity=new TestSecuritiesEntity();
            BeanUtils.copyProperties(item,securitiesEntity);
            list.add(securitiesEntity);
        }
        ExcelEasyPoiUtils.exportExcel(list, null, "数据", TestSecuritiesEntity.class, "数据", response);
    }
}